package com.llkj.qingnianguoxue.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.bean.BookVideoBean;
import com.llkj.qingnianguoxue.bean.BookVideoListBean;
import com.llkj.qingnianguoxue.home.BookDetailActivity;
import com.llkj.qingnianguoxue.home.VideoDetailActivity;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.ImageLoadingUtil;

import java.util.List;

/**
 * 订阅——视频、图书
 */
public class BookVideoAdapter extends BaseQuickAdapter<BookVideoListBean.CategoryBean, BaseViewHolder> {
    private int type = 1;//1视频，2书籍

    public BookVideoAdapter(int layoutResId, List<BookVideoListBean.CategoryBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, BookVideoListBean.CategoryBean bookVideoBean) {
        baseViewHolder.setText(R.id.tv_category_name,bookVideoBean.getCategoryName());
        LinearLayout llItems = baseViewHolder.getView(R.id.llVideos);
        llItems.removeAllViews();
        initLineItem(llItems,baseViewHolder,bookVideoBean);
    }

    /**
     * 初始化每行布局
     */
    private void initLineItem(LinearLayout llItems, BaseViewHolder baseViewHolder, BookVideoListBean.CategoryBean categoryBean){
        LinearLayout llLineItem = null;

        List<BookVideoBean> items = categoryBean.getCategoryList();
        View itemView = null;
        for(int i=0;i<items.size();i++){
            if(i%3==0){
                llLineItem = new LinearLayout(mContext);//用于添加每个视频item
                llLineItem.setOrientation(LinearLayout.HORIZONTAL);
                llItems.addView(llLineItem);
                llLineItem.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
            }
            itemView = LayoutInflater.from(mContext).inflate(R.layout.item_book_video,null);
            llLineItem.addView(itemView);
            initItemView(itemView,items.get(i),i);

            setItemClickListener(itemView,items.get(i));
        }

    }

    private void setItemClickListener(View itemView,final BookVideoBean item){
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (type == 1) {//视频
                    Intent intent = new Intent(mContext,VideoDetailActivity.class);
                    intent.putExtra("id", item.getId());
                    mContext.startActivity(intent);
                }else{//图书
                    Intent intent = new Intent(mContext,BookDetailActivity.class);
                    intent.putExtra("id", item.getId());
                    mContext.startActivity(intent);
                }
            }
        });

    }

    /**
     * 初始化每个视频Item
     */
    private void initItemView(View itemView, BookVideoBean item, int position){

        ImageView ivPic = (ImageView) itemView.findViewById(R.id.iv_video_pic);
        TextView tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        tvTitle.setText(item.getTitle());

        ImageView ivVideoPlay = (ImageView) itemView.findViewById(R.id.iv_play);
        TextView tvTime =(TextView) itemView.findViewById(R.id.tv_time_long);
        FrameLayout.LayoutParams picParams = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
        LinearLayout.LayoutParams rootParams = (LinearLayout.LayoutParams) itemView.findViewById(R.id.ll_root).getLayoutParams();
        if (type == 1) {//视频
            TextView tvPlayProgess=(TextView) itemView.findViewById(R.id.tv_progress);
            tvPlayProgess.setText("已播" + item.getPercent()+"%");
            ivVideoPlay.setVisibility(View.VISIBLE);
            tvTime.setVisibility(View.VISIBLE);
            tvTime.setText(item.getDuration());
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) tvTime.getLayoutParams();
            params.rightMargin = DisplayUtil.dip2px(mContext,10);

            picParams.width = (DisplayUtil.getDensityWdith(mContext) - DisplayUtil.dip2px(mContext, 40)) / 3;
            picParams.height = picParams.width * 142 / 182;

            if (position % 3 == 0) {
                rootParams.width = picParams.width;
                rootParams.leftMargin = 0;
            } else {
                rootParams.width = picParams.width+DisplayUtil.dip2px(mContext,5);
                rootParams.leftMargin = DisplayUtil.dip2px(mContext,5);
            }

        } else {//书籍
            TextView tvPlayProgess=(TextView) itemView.findViewById(R.id.tv_progress);
            tvPlayProgess.setText("已读" + item.getPercent());
            ivVideoPlay.setVisibility(View.INVISIBLE);
            LinearLayout llRoot = (LinearLayout) itemView.findViewById(R.id.ll_root);
            llRoot.setPadding(0, 0, 0, 0);
            picParams.width = (DisplayUtil.getDensityWdith(mContext) - DisplayUtil.dip2px(mContext, 50)) / 4;
            picParams.height = picParams.width * 142 / 130;

            if (position % 4 == 0) {
                rootParams.leftMargin = 0;
                rootParams.width = picParams.width;
            } else {
                rootParams.leftMargin = DisplayUtil.dip2px(mContext, 5);
                rootParams.width = picParams.width + DisplayUtil.dip2px(mContext,5);;
            }
            tvTime.setVisibility(View.INVISIBLE);

        }
        ImageLoadingUtil.loadImgGlideRoundCenterCrop(ivPic, item.getImg());
    }

//    @Override
//    protected void convert(BaseViewHolder baseViewHolder, BookVideoListBean.BookVideoBean item) {
//        ImageView ivPic = baseViewHolder.getView(R.id.iv_video_pic);
//        ivPic.setScaleType(ImageView.ScaleType.FIT_XY);
//        ImageLoadingUtil.loadingUrl(ivPic, item.getImg());
//        baseViewHolder.setText(R.id.tv_title, item.getTitle());
//
//        ImageView ivVideoPlay = baseViewHolder.getView(R.id.iv_play);
//        TextView tvTime = baseViewHolder.getView(R.id.tv_time_long);
//        FrameLayout.LayoutParams picParams = (FrameLayout.LayoutParams) ivPic.getLayoutParams();
//        LinearLayout.LayoutParams rootParams = (LinearLayout.LayoutParams) baseViewHolder.getView(R.id.ll_root).getLayoutParams();
//        if (type == 1) {//视频
//            baseViewHolder.setText(R.id.tv_progress, "已播" + item.getPlays_percent());
//            ivVideoPlay.setVisibility(View.VISIBLE);
//            tvTime.setVisibility(View.VISIBLE);
//            tvTime.setText(item.getDuration());
//
//            picParams.width = (DisplayUtil.getDensityWdith(mContext) - DisplayUtil.dip2px(mContext, 70)) / 2;
//            picParams.height = picParams.width * 225 / 342;
//
//            rootParams.width = (DisplayUtil.getDensityWdith(mContext) - DisplayUtil.dip2px(mContext, 30)) / 2;
//            if (baseViewHolder.getLayoutPosition() % 2 == 0) {
//                rootParams.leftMargin = 0;
//            } else {
//                rootParams.leftMargin = DisplayUtil.dip2px(mContext, 4);
//            }
//
//        } else {//书籍
//            baseViewHolder.setText(R.id.tv_progress, "已读" + item.getPlays_percent());
//            ivVideoPlay.setVisibility(View.INVISIBLE);
//            LinearLayout llRoot = baseViewHolder.getView(R.id.ll_root);
//            llRoot.setPadding(0, 0, 0, 0);
//            picParams.width = (DisplayUtil.getDensityWdith(mContext) - DisplayUtil.dip2px(mContext, 40)) / 3;
//            picParams.height = picParams.width * 301 / 222;
//
//            rootParams.width = picParams.width;
//            if (baseViewHolder.getLayoutPosition() % 3 == 0) {
//                rootParams.leftMargin = 0;
//            } else {
//                rootParams.leftMargin = DisplayUtil.dip2px(mContext, 3);
//            }
//            tvTime.setVisibility(View.INVISIBLE);
//        }
//    }

    public void setType(int type1) {
        type = type1;
    }
}
